/*
 * Copyright (C) 2008, 3apple team, All Rights Reserved
 * Author: James Cook <C.James.CJ@gmail.com>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

/* TODO:
 *	1) wrap printf. Use easy control output routine.
 *	2) HardCode
 *	3) improve the speed!!! need algorithm?? cache?? mmap?? I've no idea now.
 *	4... Mr LaiKun.. go on
 *
 * INTERNET: http://code.google.com/p/3apple-turn-over/source/detail?r=1
 */
#include "main.h"

#define CONFIG_VERSION	"1.0"
#ifdef WIN32
#define CONFIG_SYSTEM	"win32"
#else
#define CONFIG_SYSTEM	"unix"
#endif

const char *soft_version = " " \
			     CONFIG_VERSION "." CONFIG_SYSTEM " " \
			     __DATE__ " " __TIME__;
int soft_pid = -1;
int soft_debug = 0;
const char *soft_prog = "3apple translate";
const char *soft_conf_dir = "/etc/";

static void version(void)
{
	printf("%s: %s\n", soft_prog, soft_version);
	printf("Copyright (C) 2008 funny soft.\n");
	exit(0);
}

static void usage(int status)
{
	FILE *output = status?stderr:stdout;

	fprintf(output, "Usage: %s [-f conf_dir] [-h] [-v]\n", soft_prog);
	fprintf(output, "Options:\n");
	fprintf(output, "  -f  conf_dir    Set main conf directory.\n");
	fprintf(output, "  -h              Print this help message.\n");
	fprintf(output, "  -v              Print server version information.\n");
	exit(status);
}

static int soft_argcv(int argc, char **argv)
{
	int argval;

	/* process the options */
	while ((argval = getopt(argc, argv, "f:dvh")) != EOF) {
		switch (argval) {
		case 'f':
			soft_conf_dir = optarg;
			break;
		case 'd':
			soft_debug = 1;
			break;
		case 'h':
			usage(0);
			break;
		case 'v':
			version();
			break;
		default:
			usage(1);
			break;
		}
	}
	
	/* get our PID */
	soft_pid = getpid();
	return 0;
}

static int __init soft_startup(void)
{
	apple_main_init();
	return 0;
}

static void __exit soft_cleanup(void)
{
	apple_main_exit();
}

#if 1

int main(int argc, char **argv)
{	
	soft_argcv(argc, argv);
	
	if (soft_startup())
		exit(1);
	/* main loop begins here */
	apple_main_start();
	apple_main_stop();
	soft_cleanup();
	return 0;
}
#else
int main(void)
{
	return 0;
}
#endif
